package com.nsoft.html5npub.business;

import com.nsoft.html5npub.R;
import com.nsoft.html5npub.objects.Settings;
import com.nsoft.html5npub.utils.Constant;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;

public class PreferenceLoader {
	
	public static void loadSettings(Context ctx){
		Resources res=ctx.getResources();
		String keySvrName=res.getString(
					R.string.key_server_name);
		
		SharedPreferences prefs=ctx.
				getSharedPreferences(
					res.getString(
						R.string.key_server_config),
					Context.MODE_PRIVATE);
		
		if (checkPreferenceAvailable(
						keySvrName,prefs)){
			loadDefaultSettings(res,ctx);
		}else{
			copyPreferences(res,prefs);
		}
	}
	
	@SuppressLint("UseValueOf")
	private static void loadDefaultSettings(
					Resources res,Context ctx){
		SharedPreferences.Editor editor=
				ctx.getSharedPreferences(
					res.getString(
						R.string.key_server_config),
					Context.MODE_PRIVATE).edit();
		
		String uiListDefaultItem=res.getString(
							R.string.pref_ui_list); 
		
		editor.putString(
				res.getString(R.string.key_ui_list),
				uiListDefaultItem);
		Settings.setInterfaceType(uiListDefaultItem);
		
		String svrName=res.getString(
					R.string.pref_svrname);
		
		editor.putString(
			res.getString(R.string.key_server_name),
			svrName);
		Settings.setServerName(svrName);
		
		String appName=res.getString(
					R.string.pref_appname);
		
		editor.putString(
				res.getString(R.string.key_app_name),
				appName);
		Settings.setAppName(appName);
		
		String bookUrl=res.getString(
					R.string.pref_bookurl);
		
		editor.putString(
				res.getString(R.string.key_book_url),
				bookUrl);
		Settings.setDownloadUrl(bookUrl);
		
		boolean isUseAppId=new Boolean(
				res.getString(R.string.pref_use_appid).
					toUpperCase());
		
		editor.putBoolean(res.getString(
				R.string.key_use_appid),isUseAppId);
		Settings.setUseAppId(isUseAppId);
		
		String appId=res.getString(
					R.string.pref_appid);
	
		editor.putString(res.getString(
					R.string.key_appid),appId);
		Settings.setAppId(appId);
		
		String iconUrl=res.getString(
					R.string.pref_iconurl);
		
		editor.putString(
				res.getString(R.string.key_icon_url),
				iconUrl);
		Settings.setIconUrl(iconUrl);
		
		editor.commit();
	}
	
	private static boolean checkPreferenceAvailable(
			String keySvrName,SharedPreferences sp){
		return sp.getString(keySvrName,
					Constant.EMPTY).trim().
						equals(Constant.EMPTY);
	}
	
	private static void copyPreferences(
			Resources res,SharedPreferences sp){
		
		Settings.setInterfaceType(
			sp.getString(
				res.getString(R.string.key_ui_list),
				Settings.getInterfaceTypeByString()));
		
		Settings.setServerName(
			sp.getString(
				res.getString(R.string.key_server_name),
				Settings.getServerName()));
			
		Settings.setAppName(
			sp.getString(
				res.getString(R.string.key_app_name), 
				Settings.getAppName()));
			
		Settings.setDownloadUrl(
			sp.getString(
				res.getString(R.string.key_book_url),
				Settings.getDownloadUrl()));
		
		Settings.setUseAppId(sp.getBoolean(
				res.getString(R.string.key_use_appid),
				Settings.isUseAppId()));
		
		Settings.setAppId(sp.getString(
				res.getString(R.string.key_appid),
				Settings.getAppId()));
		
		Settings.setIconUrl(
			sp.getString(
				res.getString(R.string.key_icon_url),
				Settings.getIconUrl()));
	}
}
